package com.firefly.admin.monitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.annotations.annotation.OperationLog;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.monitor.entity.FyMonitorJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.firefly.admin.monitor.service.FyMonitorJobService;
import com.firefly.admin.monitor.vo.JobExecuteResult;
import com.firefly.admin.monitor.vo.MonitorJobQueryVo;
import com.firefly.admin.monitor.vo.MonitorJobStatusUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "定时任务接口API")
@RestController
@RequestMapping("/monitor/job")
public class FyMonitorJobController {

    @Autowired
    private FyMonitorJobService fyMonitorJobService;

    /**
     * 分页查询定时任务接口
     *
     * @param monitorJobQueryVo 定时任务查询条件
     * @return 定时任务信息列表
     */
    @ApiOperation(value = "分页查询定时任务接口", notes = "分页查询定时任务接口", httpMethod = "GET")
    @PreAuthorize(value = "hasAuthority('monitor:job')")
    @GetMapping("/list")
    public Result<Page<FyMonitorJob>> getJobPage(MonitorJobQueryVo monitorJobQueryVo) {
        return fyMonitorJobService.getJobPage(monitorJobQueryVo);
    }

    /**
     * 获取定时任务信息接口
     *
     * @param jobId 定时任务ID
     * @return 定时任务信息
     */
    @ApiOperation(value = "获取定时任务信息接口", notes = "通过定时任务ID获取定时任务详细信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", required = true, value = "定时任务ID", paramType = "path", dataType = "Long")
    })
    @GetMapping("/{jobId}")
    public Result<FyMonitorJob> getMonitorJob(@PathVariable Long jobId) {
        return fyMonitorJobService.getMonitorJob(jobId);
    }

    /**
     * 定时任务信息新增接口
     *
     * @param fyMonitorJob 定时任务信息
     * @return 新增结果
     */
    @ApiOperation(value = "定时任务信息新增接口", notes = "定时任务信息新增接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MONITOR, operateType = OperationTypeEnum.MONITOR_JOB, operateDesc = "定时任务新增")
    @PreAuthorize(value = "hasAuthority('monitor:job:add')")
    @PostMapping("/add")
    public Result<FyMonitorJob> add(@RequestBody FyMonitorJob fyMonitorJob) {
        return fyMonitorJobService.add(fyMonitorJob);
    }

    /**
     * 定时任务信息编辑接口
     *
     * @param fyMonitorJob 定时任务信息
     * @return 编辑结果
     */
    @ApiOperation(value = "定时任务信息编辑接口", notes = "定时任务信息编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MONITOR, operateType = OperationTypeEnum.MONITOR_JOB, operateDesc = "定时任务编辑")
    @PreAuthorize(value = "hasAuthority('monitor:job:update')")
    @PostMapping("/update")
    public Result<FyMonitorJob> update(@RequestBody FyMonitorJob fyMonitorJob) {
        return fyMonitorJobService.update(fyMonitorJob);
    }

    /**
     * 定时任务状态修改接口
     *
     * @param monitorJobStatusUpdateVo 定时任务状态编辑参数
     * @return 操作结果
     */
    @ApiOperation(value = "定时任务状态修改接口", notes = "定时任务状态修改接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MONITOR, operateType = OperationTypeEnum.MONITOR_JOB, operateDesc = "定时任务修改状态")
    @PreAuthorize(value = "hasAuthority('monitor:job:update:status')")
    @PostMapping("/update/status")
    public Result<FyMonitorJob> updateStatus(@RequestBody MonitorJobStatusUpdateVo monitorJobStatusUpdateVo) {
        return fyMonitorJobService.updateStatus(monitorJobStatusUpdateVo);
    }

    /**
     * 定时任务信息删除接口
     *
     * @param jobId 定时任务ID
     * @return 删除结果
     */
    @ApiOperation(value = "定时任务信息删除接口", notes = "定时任务信息删除接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", required = true, value = "定时任务ID", paramType = "query", dataType = "Long")
    })
    @OperationLog(module = OperationModuleEnum.SYSTEM_MONITOR, operateType = OperationTypeEnum.MONITOR_JOB, operateDesc = "定时任务删除")
    @PreAuthorize(value = "hasAuthority('monitor:job:delete')")
    @PostMapping("/delete")
    public Result<String> delete(Long jobId) {
        return fyMonitorJobService.delete(jobId);
    }

    /**
     * 手动执行任务接口
     *
     * @param jobId 定时任务ID
     * @return 执行结果
     */
    @ApiOperation(value = "手动执行任务接口", notes = "手动执行任务接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", required = true, value = "定时任务ID", paramType = "query", dataType = "Long")
    })
    @OperationLog(module = OperationModuleEnum.SYSTEM_MONITOR, operateType = OperationTypeEnum.MONITOR_JOB, operateDesc = "手动执行定时任务")
    @PreAuthorize(value = "hasAuthority('monitor:job:hand:execute')")
    @PostMapping("/hand/execute")
    public Result<JobExecuteResult> handExecute(Long jobId) {
        return fyMonitorJobService.handExecute(jobId);
    }
}
